﻿//
//
/////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using FreeSpace.Core;
using FreeSpaceLibrary;
using FreeSpaceLibrary.Objects;
using FreeSpaceLibrary.XnaContentObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Action = FreeSpaceLibrary.Action;

/////////////////////////////////////////////////////////////////////////////
namespace FreeSpace.Logic.Worker
{
    /////////////////////////////////////////////////////////////////////////
    public class TargetsField
    {
        private List<SpaceObject> _targets = new List<SpaceObject>();
        private SpriteBatch _sprite;
        private object _locker = new Object();

        /////////////////////////////////////////////////////////////////////
        public TargetsField(SpriteBatch sprite)
        {
            _sprite = sprite;
        }

        /////////////////////////////////////////////////////////////////////
        public List<SpaceObject> Targets
        {
            get { return _targets; }
        }

        /////////////////////////////////////////////////////////////////////
        public void Add(SpaceObject target)
        {
            if (target != null)
            {
                Targets.Add(target);
            }
        }

        /////////////////////////////////////////////////////////////////////
        public void AddEnemyes(ObjectData[] targets)
        {
            if(targets != null)
            {
                foreach (ObjectData target in targets)
                {
                    SpaceObject obj = new Enemy1(target, _sprite);
                    obj.OnObjectOutoffScreen += new SpaceObject.ObjectOutoffScreen(_targetOutoffScreen);
                    Targets.Add(obj);
                }
            }
        }

        /////////////////////////////////////////////////////////////////////
        private void _targetOutoffScreen(SpaceObject sender, EventArgs eventArgs)
        {
            lock (_locker)
            {
                if (_targets.Contains(sender))
                {
                    _targets.Remove(sender);
                }
            }
        }

        ///////////////////////////////////////////////////////////////////////
        //public void AddNew(TargetType type, Level level, Action action)
        //{
        //    SpaceObject target = null;
        //    switch (type)
        //    {
        //        case TargetType.Asteroid:
        //            {
        //                break;
        //            }
        //        case TargetType.Anamy1:
        //            {
        //                Thread.CurrentThread.Join(100);
        //                target = new Enemy1(_sprite);
        //                _initObjectByLevelAndAction(target, level, action);
        //                break;
        //            }
        //        case TargetType.Anamy2:
        //            {
        //                break;
        //            }
        //        default:
        //            {
        //                break;
        //            }
        //    }
        //    if (target != null)
        //    {
        //        Targets.Add(target);
        //    }
        //}

        /////////////////////////////////////////////////////////////////////
        public void Remove(SpaceObject obj)
        {
            if (obj != null && _targets.Contains(obj))
            {
                _targets.Remove(obj);
            }
        }

        ///////////////////////////////////////////////////////////////////////
        //private void _initObjectByLevelAndAction(SpaceObject spaceObject, Level level, Action action)
        //{
        //    /*note: In the future, it should be inicialized the spaceObject from the file.*/
        //    if (spaceObject != null)
        //    {
        //        float x = new Random().Next(0, Constants.ScreenWidth);
        //        float y = -spaceObject.Height;
        //        spaceObject.Position = new Vector2(x, y);
        //        spaceObject.Velocity = new Random().Next(20, 40);
        //    }
        //}

        /////////////////////////////////////////////////////////////////////
        public SpaceObject IsColissionExists(SpaceObject obj)
        {
            Vector2 vector1 = obj.Position;
            float radius1 = obj.Radius;

            foreach (SpaceObject target in Targets)
            {
                Vector2 vector2 = target.Position;
                float radius2 = target.Radius;
                float distance = 1;
                Vector2.Distance(ref vector1, ref vector2, out distance);
                if (distance <= radius1 + radius2)
                {
                    return target;
                }
            }
            return null;
        }

        /////////////////////////////////////////////////////////////////////
        public void Update(GameTime gameTime, KeyboardState keyboardState)
        {
            for (int i = 0; i < Targets.Count; i++)
            {
                SpaceObject spaceObject = Targets[i];
                spaceObject.Update(gameTime, keyboardState);
            }
        }

        /////////////////////////////////////////////////////////////////////
        public void Draw()
        {
            foreach (SpaceObject spaceObject in _targets)
            {
                spaceObject.Draw();
            }
        }
    }
/////////////////////////////////////////////////////////////////////////////
}//
